package com.macro.mall.tiny.modules.foster.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.foster.model.Pet;
import com.macro.mall.tiny.modules.foster.model.dto.PetAndTimeDTO;
import com.macro.mall.tiny.modules.foster.service.PetService;
import com.macro.mall.tiny.modules.foster.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhuyi
 * @since 2024-03-27
 */
@Api(tags = "PetController", description = "寄养宠物信息管理")
@RestController
@RequestMapping("/foster/pet")
@CrossOrigin
public class PetController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PetController.class);

    @Autowired
    private PetService petService;
    @Autowired
    private UserService userService;

    /**
     * 添加寄养宠物
     *
     * @param
     * @return
     */
    @ApiOperation(value = "添加寄养宠物和预约信息")
    @RequestMapping(value = "/addPet", method = RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('user','admin')")
    public CommonResult addPetAndAppointment(@RequestBody PetAndTimeDTO petAndTimeDTO) {
        return petService.savePetAndAppointment(petAndTimeDTO);
    }

    /**
     * 删除寄养宠物
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除寄养宠物")
    @RequestMapping(value = "/deletePetById/{id}", method = RequestMethod.DELETE)
    @PreAuthorize("hasAnyAuthority('user','admin')")
    public CommonResult deletePetById(@PathVariable Long id) {
        return petService.removeByPetId(id);
    }

    /**
     * 修改寄养宠物
     *
     * @param pet
     * @return
     */
    @ApiOperation(value = "修改寄养宠物")
    @RequestMapping(value = "/updatePet", method = RequestMethod.PUT)
    @PreAuthorize("hasAnyAuthority('user','admin')")
    public CommonResult updatePet(@RequestBody Pet pet) {
        petService.updateById(pet);
        LOGGER.info("修改寄养宠物成功");
        return CommonResult.success("修改寄养宠物成功");
    }

    /**
     * 分页查询宠物列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询宠物列表")
    @PreAuthorize("hasAnyAuthority('user','admin')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Pet>> list(@RequestParam(value = "pageNum", defaultValue = "1")
                                                   @ApiParam("页码") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "5")
                                                   @ApiParam("每页数量") Integer pageSize) {
        Page<Pet> page = new Page<>(pageNum, pageSize);
        Page<Pet> pageResult = petService.pagePet(page);
        LOGGER.info("宠物类型列表查询状态：查询成功");
        return CommonResult.success(CommonPage.restPage(pageResult));
    }
    /**
     * 根据用户id获取宠物信息
     * @param id
     * @return
     */
    @ApiOperation("根据用户id获取宠物信息")
    @RequestMapping(value = "/getPetByUserId/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('user','admin')")
    public CommonResult<Pet> getPetByUserId(@PathVariable("id") Long id) {

        return petService.getByUserId(id);
    }

    /**
     * 根据id获取宠物信息
     * @param id
     * @return
     */
    @ApiOperation("获取指定id的寄养宠物信息")
    @RequestMapping(value = "/getPetById/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('user','admin')")
    public CommonResult<Pet> getPetById(@PathVariable("id") Long id) {
        Pet pet = petService.getByPetId(id);
        LOGGER.info("根据id获取寄养宠物成功");
        return CommonResult.success(pet);
    }


}
